#!/usr/bin/ruby
# -*- coding: undecided -*-
#

require 'nkf'
require 'uri'
require 'mysql'
require 'rexml/document'
require 'net/http'
require 'date'

require 'aws'
require '../db_setting'


def get_text(node)
  if node != nil then
    return node.text
  else
    return ""
  end
end

def get_int(node)
  if node != nil then
    return node.text.to_i
  else
    return 0
  end
end


def db_add_item(node, db)
  asin              = get_text(node.elements["ASIN"])
  detail_page_url   = get_text(node.elements["DetailPageURL"])
  title             = get_text(node.elements["ItemAttributes/Title"])
  amount            = get_int(node.elements["ItemAttributes/ListPrice/Amount"])
  publication_date  = get_text(node.elements["ItemAttributes/PublicationDate"])
  publisher         = get_text(node.elements["ItemAttributes/Publisher"])
  author            = get_text(node.elements["ItemAttributes/Author"])
  sales_rank        = get_int(node.elements["ItemAttributes/SalesRank"])
  number_of_pages   = get_int(node.elements["ItemAttributes/NumberOfPages"])

  pub_year, pub_month, pub_day = publication_date.split("-")
  if pub_day == nil then
    pub_day = 0
  end

  sql =
    'insert into books (asin, detail_page_url, author, title, amount,' +
    ' publication_year, publication_month, publication_day, publisher, number_of_pages, sales_rank) values ' +
    "('#{asin}', '#{detail_page_url}', '#{author.gsub(/'/, ' ')}', '#{title.gsub(/'/, ' ')}', #{amount}," +
    "#{pub_year}, #{pub_month}, #{pub_day}, '#{publisher.gsub(/'/, ' ')}', #{number_of_pages}, #{sales_rank})"
  begin
    db.query(sql)
  rescue
    puts "ERROR: " + sql
    return
  end
  node.elements.each("SimilarProducts/SimilarProduct/ASIN") do |node2|
    dst_asin = node2.text
    sql =
      'insert into link_book2book (src_asin, dst_asin) values' +
      "('#{asin}', '#{dst_asin}')"
    begin
      db.query(sql)
    rescue
    end
  end

  node.elements.each("BrowseNodes/descendant::BrowseNodeId") do |node2|
    browse_node_id = node2.text.to_i
    sql =
      'insert into link_book2browse_node (asin, browse_node_id) values' +
      "('#{asin}', '#{browse_node_id}')"
    begin
      db.query(sql)
    rescue
    end
  end

end


def search_item(browse_node, date, db)
  total_pages = 0
  page = 0
  begin
    page += 1
    Net::HTTP.start(AWS_URL, 80) do |http|
      req = [
             'Service=AWSECommerceService',
             "AWSAccessKeyId=#{AWS_ACCESS_KEY_ID}",
             "Version=#{AWS_VERSION}",
             "Operation=ItemSearch",
             "ResponseGroup=ItemAttributes%2CBrowseNodes%2CSimilarities%2CSalesRank",
             "BrowseNode=#{browse_node}",
             "SearchIndex=Books",
             "ItemPage=#{page}",
             "Timestamp=#{Time.now.getgm.strftime('%Y-%m-%dT%H%%3A%M%%3A%SZ')}",
             "Power=pubdate%3Aduring%20#{date}"
            ].sort.join("&")
      signed_url = aws_signed_url(req)

      res = http.get(signed_url).body

      ct = 0
      doc = REXML::Document.new(res)
      
      if total_pages == 0 then
        begin
          total_pages = doc.elements["//TotalPages"].text.to_i
          STDERR.puts "#{date}: browse_node: #{browse_node} total_pages: #{total_pages}"
          if total_pages > 400 then
            puts "ERROR: #{date}: browse_node: #{browse_node} total_pages: #{total_pages}"
          end
        rescue => exc
          puts "error : '#{res}'"
          exit
        end
      end
      
      doc.elements.each("//Item") do |node|
        db_add_item(node, db)
        ct += 1
      end
      
      if ct == 0 then
        puts 'ERROR: date:%s page:%d' % [date, page]
      end
    end
  end while page < total_pages
end


month = 7
year = 2009

begin
  db = Mysql::new(DB_HOST, DB_USER, DB_PASS, DB_NAME)
  res = db.query("select * from link_browse_node2browse_node where parent_id = 465610")
  res.each_hash do |row|
    search_item(row["child_id"], "%02d-%d" % [month, year], db)
  end

  break                         # test

  month -= 1
  if month == 0 then
    month = 12
    year -= 1
  end
end while year >= 1900

